package com.mac.cxcybackground.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mac.cxcybackground.bean.Position;
import com.mac.cxcybackground.service.PositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class PositionController {
    @Autowired
    PositionService positionService;
    @GetMapping("/position_table")
    public String PositionFind(@RequestParam(value = "pn",defaultValue = "1")Integer pn,
                               Model model){
        Page<Position> positionPage = new Page<>(pn, 8);
        Page<Position> page = positionService.page(positionPage,null);
        model.addAttribute("page",page);
        return "table/position_table";
    }
    @GetMapping("/position_add_table")
    public String position_add_table(){
        return "table/position_add_table";
    }
    @PostMapping("/position/add")
    public String PositionAdd(Position position,Model model){
        Position p = new Position(null,position.getSName(),position.getPName());
        positionService.save(p);
        model.addAttribute("success","添加成功");
        return "redirect:/position_table";
    }
    @GetMapping("/position/delete/{id}")
    public String PositionDelete(@PathVariable("id") Long id,
                                 @RequestParam(value = "pn",defaultValue = "1") Integer pn,
                                 RedirectAttributes ra){
        positionService.removeById(id);
        ra.addAttribute("pn",pn);
        return "redirect:/position_table";
    }
    @GetMapping("/position/update-get/{id}")
    public String PositionUpdate_get(@PathVariable("id") Long id,Model model,
                                     @RequestParam(value = "pn",defaultValue = "1") Integer pn,
                                     RedirectAttributes ra){
        Position result = positionService.getById(id);
        ra.addAttribute("pn",pn);
        model.addAttribute("result",result);
        return "table/position_update_table";
    }
    @GetMapping("/position/update/{id}")
    private String PositionUpdate(@PathVariable("id") Long id,Position position){
        Position p = new Position(id,position.getSName(),position.getPName());
        positionService.updateById(p);
        return "redirect:/position_table";
    }
}
